<?php

namespace app\api\controller;

class Base
{

    //注册功能
    public function register()
    {
        //传入学号，姓名，密码注册
        $on = input('get.on');
        $password = input(md5('get.password'));
        $name = input('get.name');
        //规则1：学号唯一
        //规则2：密码md5加密
    }

    //找回密码
    public function findpass()
    {
        //通过学号，姓名重置密码
        //规则1：密码md5加密
        $on = input('get.on');
        $name = input('get.name');
        $password = input('get.password');
        //第二步：校验学号与姓名是否匹配
        //SELECT * FROM member WHERE member_mobile = '15207713074' and  member_nickname= '123456';
        $condition = array();
        $condition[] = ['student_mobile', '=', $on];
        $condition[] = ['student_name', '=', $name];
        $data = Member::where($condition)->find();//FIND函数只查一条，返回的数据对象
        if (empty($data)) output_error('学号与姓名不匹配，无法修改密码');
        //第三步：更新用户的密码
        //UPDATE member SET member_password = '123456' WHERE member_id  = 1;
        Member::where('student_id', '=', $data['student_id'])->update(array('student_password' => md5($password)));
        output_data(1);
    }

    //登录功能
    public function login()
    {
        //通过学号，密码登录，返回Token
        //规则1：token由学号+当前时间戳md5加密生成
       //第一步：接收必要的参数，并完成参数的校验
        $on = input('get.on');
        $password = input(md5('get.password'));
        $name = input('get.name');
        //第二步：使用账号与密码去数据库匹配
        $condition = array();
        $condition[] = ['student_on', '=', $on];
        $condition[] = ['student_password', '=', md5($password)];
        $member = Member::where($condition)->find();
        if (empty($member)) output_error('账号或密码错误');
        //第三步：匹配成功，生成TOKEN返回给客户端，匹配失败，提示账号或密码错误
        $token = md5($member['student_id'] . time());
        $data = array();
        $data['student_id'] = $member['student_id'];
        $data['student_token'] = $token;
        $data['student_on'] = $on;
        $data['add_time'] = time();
        MemberToken::insert($data);
        output_data($token);
    }

}
